package com.xbongbong.pro.message.controller;/*/*/

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.message.pojo.dto.MessageExpiredReminderDTO;
import com.xbongbong.pro.message.pojo.dto.MessageSaveAllTargetDataDTO;
import com.xbongbong.pro.message.service.MessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;


/**
 * 用于消息的定时任务消息提醒入库 （客户自定义消息，合同到期提醒，销售机会到期提醒（/businessToPush），销售简报消息，未回款提醒，逾期回款提醒（PushDataThread））
 * @author 魏荣杰
 * @date 2019/2/13 9:08
 * @since v1.0
 * @version v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/promessage/remind")
public class MessageRemindController {

    private static final Logger LOG = LoggerFactory.getLogger(MessageRemindController.class);

    @Resource
    private MessageService messageService;

    /**
     * Description: 客户自定义消息，合同到期提醒，销售机会到期提醒 报价单到期提醒 消息数据入库
     * @param messageSaveAllTargetDataDTO
     * @param br
     * @return java.lang.String
     * @author 魏荣杰
     * @date 2019/2/14 11:14
     * @since v1.0
     */
    @RequestMapping(value = "/businessToPush", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String businessToPush( @Valid MessageSaveAllTargetDataDTO messageSaveAllTargetDataDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                messageService.businessToPush(messageSaveAllTargetDataDTO);
                BaseVO baseVO = new BaseVO();
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * Description: 销售简报统计数据入库
     * @param messageSaveAllTargetDataDTO
     * @param br
     * @return java.lang.String
     * @author 魏荣杰
     * @date 2019/2/14 11:15
     * @since v1.0
     */
    @RequestMapping(value = "/saveAllTargetData", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String saveAllTargetData( @Valid MessageSaveAllTargetDataDTO messageSaveAllTargetDataDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                messageService.saveAllTargetData(messageSaveAllTargetDataDTO);
                BaseVO baseVO = new BaseVO();
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * Description: 销售简报消息，未回款提醒，逾期回款提醒 消息数据入库
     * @param messageSaveAllTargetDataDTO
     * @param br
     * @return java.lang.String
     * @author 魏荣杰
     * @date 2019/2/14 11:16
     * @since v1.0
     */
    @RequestMapping(value = "/saveAllPushData", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String saveAllPushData( @Valid MessageSaveAllTargetDataDTO messageSaveAllTargetDataDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                messageService.saveAllPushData(messageSaveAllTargetDataDTO);
                BaseVO baseVO = new BaseVO();
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * Description: 套餐到期提醒数据入库
     * @return java.lang.String
     * @author 魏荣杰
     * @date 2019/2/14 11:17
     * @since v1.0
     */
    @RequestMapping(value = "/saveExpiredReminder", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String saveExpiredReminder(@Valid MessageExpiredReminderDTO messageExpiredReminderDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                messageService.saveExpiredReminder(messageExpiredReminderDTO);
                BaseVO baseVO = new BaseVO();
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }


        return JSON.toJSONString(response);
    }

    /**
     * Description: 工作报告未交提醒，工作报告逾期未交提醒
     * @param messageSaveAllTargetDataDTO
     * @param br
     * @return java.lang.String
     * @author long.rao
     * @date 2020-07-28 17:29
     */
    @RequestMapping(value = "/workReportPush", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String workReportPush( @Valid MessageSaveAllTargetDataDTO messageSaveAllTargetDataDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                messageService.workReportRemindPush(messageSaveAllTargetDataDTO);
                BaseVO baseVO = new BaseVO();
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    
}
